<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\Message as MessageModel;
use app\user\model\User as UserModel;
use app\user\model\Role as RoleModel;
use app\user\model\Level;

/**
 * 消息控制器
 * @package app\user\admin
 */
class Message extends Admin
{
    /**
     * 消息列表
     * @author PaulXun <paulxun@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data_list = MessageModel::where($this->getMap())
            ->order($this->getOrder('id DESC'))
            ->paginate(config('paginate'));

        return ZBuilder::make('table')
            ->setTableName('admin_message')
            ->addTopButton('add')
            ->addTopButton('delete')
            ->addRightButton('edit')
            ->addRightButton('delete')
            ->addColumns([
                ['id', 'ID'],
                ['uid_receive', '接收者', 'callback', function($value, $data){if($data['send_type'] != 'vip') {return get_nickname($value);} else {return Level::level_list($value);}}, '__data__'],
                ['uid_send', '发送者', 'callback', 'get_nickname'],
                ['type', '分类'],
                ['content', '内容'],
                // ['status', '状态', 'status', '', ['未读', '已读']],
                ['create_time', '发送时间', 'datetime'],
                // ['read_time', '阅读时间', 'datetime'],
                ['right_button', '操作', 'btn'],
            ])
            ->addFilter('type')
            // ->addFilter('status', ['未读', '已读'])
            ->setRowList($data_list)
            ->fetch();
    }

    /**
     * 新增
     * @author PaulXun <paulxun@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $data['type'] == '' && $this->error('请填写消息分类');
            $data['content'] == '' && $this->error('请填写消息内容');

            $list = [];
			if ($data['send_type'] != 'vip') {
				if ($data['send_type'] == 'uid') {
				    !isset($data['uid']) && $this->error('请选择接收消息的用户');
				} else {
				    !isset($data['role']) && $this->error('请选择接收消息的角色');
				    $data['uid'] = UserModel::where('status', 1)
				        ->where('role', 'in', $data['role'])
				        ->column('id');
				    !$data['uid'] && $this->error('所选角色无可发送的用户');
				}
				
				foreach ($data['uid'] as $uid) {
				    $list[] = [
				        'uid_receive' => $uid,
				        'uid_send'    => UID,
				        'type'        => $data['type'],
				        'content'     => $data['content'],
						'send_type'	  => $data['send_type'],
				    ];
				}
			} else {
				!isset($data['group']) && $this->error('请选择接收消息的会员组');
				foreach ($data['group'] as $uid) {
				    $list[] = [
				        'uid_receive' => $uid,
				        'uid_send'    => UID,
				        'type'        => $data['type'],
				        'content'     => $data['content'],
						'send_type'	  => $data['send_type'],
				    ];
				}
			}
            $MessageModel = new MessageModel;
            if (false !== $MessageModel->saveAll($list)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'type', '消息分类'],
                ['textarea', 'content', '消息内容'],
                ['radio', 'send_type', '发送方式', '', ['uid' => '按管理员用户', 'role' => '按后台角色', 'vip' => '按会员等级'], 'uid'],
                ['select', 'uid', '接收用户', '接收消息的用户', UserModel::where('status', 1)->column('nickname', 'id'), '', 'multiple'],
                ['select', 'role', '接收角色', '接收消息的角色', RoleModel::where('status', 1)->column('name', 'id'), '', 'multiple'],
				['select', 'group', '接收会员组', '接收消息的Vip会员组', Level::level_list(), '', 'multiple'],
            ])
			->setTrigger('send_type', 'vip', 'group')
            ->setTrigger('send_type', 'uid', 'uid')
            ->setTrigger('send_type', 'role', 'role')
            ->fetch();
    }
	
	/**
	 * 编辑
	 * @author PaulXun <paulxun@qq.com>
	 * @return mixed
	 * @throws \think\Exception
	 */
	public function edit($id = 0)
	{
		if ($id === 0) $this->error('参数错误');
		$info = MessageModel::find($id);
	    if ($this->request->isPost()) {
	        $data = $this->request->post();
	
	        $data['type'] == '' && $this->error('请填写消息分类');
	        $data['content'] == '' && $this->error('请填写消息内容');
			$info->type = $data['type'];
	        $info->content = $data['content'];
	        if ($info->save()) {
	            $this->success('编辑成功', 'index');
	        } else {
	            $this->error('保存失败');
	        }
	    }
		
		$info['uid'] = $info['uid_receive'];
		$info['role'] = $info['uid_receive'];
		$info['group'] = $info['uid_receive'];
	    return ZBuilder::make('form')
	        ->addFormItems([
	            ['text', 'type', '消息分类'],
	            ['textarea', 'content', '消息内容'],
	            ['radio', 'send_type', '发送方式', '', ['uid' => '按管理员用户', 'role' => '按后台角色', 'vip' => '按会员等级'], '', '', 'disabled'],
	            ['checkbox', 'uid', '接收用户', '接收消息的用户', UserModel::where('status', 1)->column('nickname', 'id'), '', '', 'disabled'],
	            ['checkbox', 'role', '接收角色', '接收消息的角色', RoleModel::where('status', 1)->column('name', 'id'), '', '', 'disabled'],
				['checkbox', 'group', '接收会员组', '接收消息的Vip会员组', Level::level_list(), '', '', 'disabled'],
	        ])
			->setTrigger('send_type', 'vip', 'group')
	        ->setTrigger('send_type', 'uid', 'uid')
	        ->setTrigger('send_type', 'role', 'role')
			->setFormData($info) // 设置表单数据
	        ->fetch();
	}
}
